<?php
require_once 'mysql.php';


abstract class AbstractDAO{
	protected $connection;
	public function __construct(){
		global $config_mysql_database;
		$this->connection = $this->connect();
		mysql_set_charset("utf8", $this->connection);
		mysql_select_db($config_mysql_database);
	}
	public function __destruct(){
		$this->disconnect();
	}
	protected function connect(){
		global $config_mysql_server;
		global $config_mysql_username;
		global $config_mysql_password;
		return mysql_connect($config_mysql_server, $config_mysql_username, $config_mysql_password);
	}
	protected function disconnect(){
		mysql_close($this->connection);
	}
	public function commit(){
		return $this->query("commit");
	}
	public function rollback(){
		return $this->query("rollback");
	}
	public function query($q){
		Logger::debug("QUERY [\n".$q."\n]");
		return mysql_query($q, $this->connection);
	}
	protected function insert($table, $values){
		
		$fieldCount = 0;
		$fieldString = "";
		$valueString = "";
		foreach ($values as $field => $value){
			
			$fieldString.= "\t";
			if ($fieldCount > 0)
				$fieldString.=",";
			$fieldString.= "$field\n";
			
			$valueString.= "\t";
			if ($fieldCount > 0)
				$valueString.=",";
			$valueString.= "'$value'\n";
			$fieldCount++;
		}
		$q = "INSERT INTO $table (\n$fieldString\n)\nVALUES(\n$valueString\n)";
		
		return $this->query($q);
	}
	protected function update($table, $updateDatas, $condition = null){
		
		$fieldCount = 0;
		$fieldString = "";
		foreach ($updateDatas as $field => $value){
			
			$fieldString.= "\t";
			if ($fieldCount > 0)
				$fieldString.=",";
			$fieldString.= "$field = '$value'\n";
			
			$fieldCount++;
		}
		$q = "UPDATE $table SET \n$fieldString\n WHERE\n\t$condition";
		
		return $this->query($q);
	}
	protected function select($table, $fields, $condition = null){
		$q = "SELECT\n";
		
		$fieldCount = 0;
		if (is_array($fields)){
			foreach ($values as $field){
				$q.= "\t";
				if ($fieldCount > 0)
					$q.=",";
				$q.= $field."\n";
			}
		}
		else{
			$q.= $fields;
		}
		
		$q.= "\nFROM\n";
		$q.= $table."\n";
		
		if ($condition != null){
			$q.= "WHERE\n";
			$q.= "\t".$condition;
		}
		
		return $this->query($q);
	}
	protected function selectOne($table, $fields, $condition = null){
		$result = $this->select($table, $fields, $condition);
		if ($result)
			return mysql_fetch_assoc($result);
		else
			return null;
	}
	protected function selectList($table, $fields, $condition = null){
		$result = $this->select($table, $fields, $condition);
		if ($result){
			$count = mysql_num_rows($result);
			$list = array();
			for ($i = 0 ; $i < $count ; $i++){
				array_push($list, mysql_fetch_assoc($result));
			}
			return $list;
		}
		else
			return null;
	}
	protected function selectMax($table, $maxField, $condition = null){
		$result = $this->selectOne($table, "MAX($maxField) $maxField", $condition);
		if ($result == null)
			return null;
		return $result[$maxField];
	}
}
?>